package com.scau.yangsuiyu.estore.service;

//import com.scau.yangsuiyu.estore.dababases.entity.Orders;

import com.alibaba.fastjson.JSONObject;
import com.scau.yangsuiyu.estore.dababases.dao.GoodsDao;
import com.scau.yangsuiyu.estore.dababases.dao.ItemDao;
import com.scau.yangsuiyu.estore.dababases.dao.OrderDao;
import com.scau.yangsuiyu.estore.dababases.dao.OrderDetailDao;
import com.scau.yangsuiyu.estore.dababases.entity.Item;
import com.scau.yangsuiyu.estore.dababases.entity.Orders;
import com.scau.yangsuiyu.estore.dababases.entity.OrderDetail;
import com.scau.yangsuiyu.estore.util.OrderStatus;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author suiyu_yang
 * @description 订单的业务逻辑实现
 * @date 2019-06-13 15:01
 * @email suiyu_yang@163.com
 */
public class OrderService {
    private Orders orders;

    public OrderService(Orders orders) {
        this.orders = orders;
    }

    /**
     * 创建订单
     *
     * @param itemList 订单项
     */
    public JSONObject createOrder(List<Item> itemList) {
        JSONObject resultJson = new JSONObject();
        setDefaultValue();

        if (confirmInventory(itemList)) { //购买的数量均比库存量少，可以购买
            new OrderDao().insert(orders);
            for (Item item : itemList) {
                new ItemDao().insert(item);

                OrderDetail orderDetail = new OrderDetail();
                orderDetail.setItemId(item.getId());
                orderDetail.setOrderId(orders.getId());
                new OrderDetailDao().insert(orderDetail);

                int inventory = item.getGoods().getInventory();
                inventory -= item.getAmount();
                item.getGoods().setInventory(inventory);
                new GoodsDao().update(item.getGoods());
            }
            resultJson.put("status", "success");
            resultJson.put("result", "添加订单成功！");
            return resultJson;
        } else {
            resultJson.put("status", "failed");
            resultJson.put("result", "库存不足！");
            return resultJson;
        }
    }

    /**
     * 买家评论后，订单结束
     */
    public void buyerEndOrder() {
        Timestamp currentTimeStamp = new Timestamp(System.currentTimeMillis());
        orders.setEndTime(currentTimeStamp);
        new OrderDao().update(orders);
    }

    /**
     * 卖家收货
     */
    public void sellerDelivery() {
        Timestamp currentTimeStamp = new Timestamp(System.currentTimeMillis());
        orders.setDeliveryTime(currentTimeStamp);

        new OrderDao().update(orders);
    }

    private void setDefaultValue() {
        Timestamp currentTimeStamp = new Timestamp(System.currentTimeMillis());
        orders.setCreateTime(currentTimeStamp);
        orders.setStatus(OrderStatus.UN_DELIVERED);
    }

    /**
     * 确认购买的所有商品数目均少于库存量
     *
     * @return
     */
    private boolean confirmInventory(List<Item> itemList) {
        for (Item item : itemList) {
            if (item.getAmount() > item.getGoods().getInventory()) { //购买的数量比库存量少，可以购买
                return false;
            }
        }
        return true;
    }
}
